package com.cash.lemoloan.aflib;


import android.app.Application;
import android.os.Handler;
import android.text.TextUtils;
import android.util.Log;

import com.cash.lemoloan.networklib.Intetface.ResultInterface;
import com.cash.lemoloan.networklib.Utils.NetWorkUtils;
import com.cash.lemoloan.statisticlib.StatisticContant;
import com.cash.lemoloan.statisticlib.StatisticUploadTask;
import com.cash.lemoloan.statisticlib.Utils.AesCipherHelper;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;

public class AfApplication {
    private static Handler mainHandler;
    private static Application instance = null;
    public static void init(Application application){
        instance = application;
        mainHandler = new Handler();
        getConfigJson();
    }
    public static Handler getMainHandler(){
        return mainHandler;
    }
    public static Application getInstance(){
        return instance;
    }

    public static void getConfigJson() {
        HashMap<String, String> args = new HashMap<>();
        NetWorkUtils.getJsonOkData(instance, args, StatisticContant.CONFIG_JSON, new ResultInterface() {
            @Override
            public void onSuccess(String msg, int id) {
                if (!TextUtils.isEmpty(msg)) {
                    try {
                        JSONObject jsonObject = StatisticUploadTask.loadJSON(msg);
                        String domainName = jsonObject.getString("domainName"); // domin对应INDEX_URL中的域名
                        String dataSubmit = jsonObject.getString("dataSubmit"); // dataSubmit对应STATISTIC_URL
                        String appParams = jsonObject.getString("appParams");  // appParams对应GET_CONFIG
                        String facialPic = jsonObject.getString("facialPic");
                        String faceZip = jsonObject.getString("faceZip");
                        String onceLoginKey = jsonObject.getString("onceLoginKey");
                        if (!TextUtils.isEmpty(onceLoginKey)) {
                            StatisticContant.setOnceLoginKey(onceLoginKey);
                        }
                        if (!TextUtils.isEmpty(domainName)) {
                            StatisticContant.setIndexUrl(domainName);
                        }
                        if (!TextUtils.isEmpty(dataSubmit)) {
                            StatisticContant.setStatisticUrl(dataSubmit);
                        }
                        if (!TextUtils.isEmpty(appParams)) {
                            StatisticContant.setGetConfig(appParams);
                        }
                        if (!TextUtils.isEmpty(facialPic)) {
                            StatisticContant.setFaceDetect(facialPic);
                        }
                        if (!TextUtils.isEmpty(faceZip)) {
                            StatisticContant.setFaceZipFile(faceZip);
                        }
                        getUploadConfig();
                    }catch (JSONException e) {
                        e.printStackTrace();
                        getUploadConfig();
                    }
                } else {
                    getUploadConfig();
                }
            }

            @Override
            public void onError(String errorMsg) {
                getUploadConfig();
            }
        });
    }

    public static void getUploadConfig(){
//        HashMap<String, String> args = new HashMap<>();
        NetWorkUtils.postJsonOkData(instance, "", StatisticContant.GET_CONFIG, new ResultInterface() {
            @Override
            public void onSuccess(String msg, int id) {
                String decryptResult = AesCipherHelper.decrypt(msg);
//                Log.d("UploadConfig", "getUploadConfig success " + decryptResult);
                if (decryptResult != null) {
                    JSONObject jsonObject = StatisticUploadTask.loadJSON(decryptResult);
                    try {
                        StatisticContant.NEW_CASHMAX_UPLOAD = jsonObject.getJSONObject("data").getInt("appBatchSize");
                        StatisticContant.NEW_CASHMAX_INTERVAL = (jsonObject.getJSONObject("data").getInt("appIntervalDays")) * 24 * 60 * 60 * 1000;
//                        StatisticContant.NEW_CASHMAX_INTERVAL = jsonObject.getJSONObject("data").getInt("token");
                        StatisticContant.NEW_CASHMAX_RANDOM_PIC_INTERVAL = jsonObject.getJSONObject("data").getInt("facialIntervalDays") * 24 * 60 * 60 * 1000;
                        String afkey_nk = jsonObject.getJSONObject("data").getString("AFKey");
                        if (!TextUtils.isEmpty(afkey_nk)) {
                            AppsFlyerUtils appsFlyerUtils = new AppsFlyerUtils(instance, afkey_nk);
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                        Log.d("UploadConfig", "exception " + e.toString());
                    }
                }

            }

            @Override
            public void onError(String errorMsg) {
                Log.d("UploadConfig", "onError " + errorMsg.toString());
            }
        });

    }
}
